package com.test.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * 学习休息时间提醒自动打开qq音乐
 */
public class MyMusic {
    public static void main(String[] args) {
        //打开cmd执行
        //javac -encoding utf-8 MyMusic.java
        //java MyMusic

        try {
            //学习25分钟
            int study = 1000 * 60 * 25;
            //休息5分钟
            int rest = 1000 * 60 * 5;
            //累计学习次数
            int i = 0;
            Runtime rt = Runtime.getRuntime();
            Process p = null;
            String fileLac = "";
            while (true) {
                i++;
                if (i == 4) {
                    //休息15分钟
                    rest = 1000 * 60 * 15;
                }
                //打开qq音乐
                Thread.sleep(study);
                fileLac = "D:\\软件\\360安装\\tencent\\qqmusic\\QQMusic.exe";// 要调用的程序路径
                p = rt.exec(fileLac);

                Thread.sleep(rest);
                //关闭
                close();
                rest = 1000 * 60 * 5;
            }

        } catch (Exception e) {
            System.out.println("open failure");
        }
    }

    private static void close() throws Exception {
        //关闭
        String[] cmd =
                {"tasklist"};
        Process proc = Runtime.getRuntime().exec(cmd);
        BufferedReader in = new BufferedReader(new InputStreamReader(proc
                .getInputStream()));
        String string_Temp = in.readLine();
        while (string_Temp != null) {
            System.out.println(string_Temp);
            if (string_Temp.indexOf("QQMusic.exe") != -1) {
                System.out.println("00000");
                Runtime.getRuntime().exec("taskkill /f /t /im QQMusic.exe");
            }
            string_Temp = in.readLine();
        }
    }
}
